{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "std::cout are redirected to python::stdout\n",
      "std::cerr are redirected to python::stderr\n",
      "[2019-03-27 21:33:13.813] [info] Loading market information...\n",
      "[2019-03-27 21:33:13.816] [info] Loading stock type information...\n",
      "[2019-03-27 21:33:13.818] [info] Loading stock information...\n",
      "[2019-03-27 21:33:18.709] [info] Loading KData...\n",
      "[2019-03-27 21:33:18.720] [info] Preloading all day kdata to buffer!\n",
      "[2019-03-27 21:33:27.778] [info] 9.068s Loaded Data.\n",
      "Wall time: 16.6 s\n"
     ]
    }
   ],
   "source": [
    "%matplotlib inline\n",
    "%time from hikyuu.interactive.interactive import *"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1 全局获取股票对象\n",
    "==========\n",
    "\n",
    "1.1 获取股票对象\n",
    "-----------------\n",
    "\n",
    "通过全局管理对象 sm，或使用函数 getStock。股票标识格式“市场标识+股票代码”，市场标识：沪市sh，深市sz。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Stock(SH, 000001, 上证指数, 指数, 1, 1990-12-19 0:0:0, +infinity)\n"
     ]
    }
   ],
   "source": [
    "#s = getStock('sh000001')\n",
    "s = sm['sh000001']\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1.2 遍历所有股票\n",
    "-----------------"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "全部数量: 5517\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "5517"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "i = 0\n",
    "#遍历所有股票\n",
    "for s in sm:\n",
    "    i += 1\n",
    "    #print(s)\n",
    "print(\"全部数量:\", i)\n",
    "\n",
    "len(sm)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2 通过板块（Block）遍历股票对象\n",
    "================\n",
    "\n",
    "2.1 通过 sm.getStock(\"板块分类\", \"板块名称\") 获取相应板块\n",
    "------------------------------------------------------------"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Stock(SH, 600270, 外运发展, A股, 0, 2000-12-28 0:0:0, +infinity)\n"
     ]
    }
   ],
   "source": [
    "blk = sm.getBlock(\"指数板块\", \"上证380\")\n",
    "for s in blk:\n",
    "    if not s.valid:\n",
    "        print(s)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2.1 获取自定义板块\n",
    "------------------\n",
    "\n",
    "自定义板块的板块分类固定为 “self”"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Stock(SZ, 002685, 华东重机, A股, 1, 2012-6-12 0:0:0, +infinity)\n",
      "Stock(SZ, 002339, 积成电子, A股, 1, 2010-1-22 0:0:0, +infinity)\n",
      "Stock(SZ, 000728, 国元证券, A股, 1, 1997-5-22 0:0:0, +infinity)\n",
      "Stock(SZ, 000958, 东方能源, A股, 1, 1999-12-23 0:0:0, +infinity)\n",
      "Stock(SZ, 000001, 平安银行, A股, 1, 1991-1-2 0:0:0, +infinity)\n",
      "Stock(SH, 600601, 方正科技, A股, 1, 1990-12-19 0:0:0, +infinity)\n",
      "Stock(SH, 600050, 中国联通, A股, 1, 2002-10-9 0:0:0, +infinity)\n",
      "Stock(SH, 601018, 宁波港, A股, 1, 2010-9-28 0:0:0, +infinity)\n",
      "Stock(SH, 601098, 中南传媒, A股, 1, 2010-10-28 0:0:0, +infinity)\n"
     ]
    }
   ],
   "source": [
    "blk = sm.getBlock(\"self\", \"1\")\n",
    "for s in blk:\n",
    "    print(s)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2.2 板块信息的配置\n",
    "-------------------\n",
    "\n",
    "板块信息在数据存放路径中 “block” 子目录下，目前采用的是钱龙的格式，你也可从钱龙相应的目录下拷贝最新的板块配置信息。\n",
    "\n",
    "![板块配置](images/002_01_block_config.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3 查看权息信息\n",
    "======="
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Weight(1991-4-3 0:0:0, 0, 0, 0, 0, 0, 150, 68)\n",
      "Weight(1993-5-24 0:0:0, 3.5, 1, 16, 3, 5, 26941, 17912)\n",
      "Weight(1994-7-11 0:0:0, 3, 1, 5, 5, 2, 43106, 28659)\n",
      "Weight(1994-9-2 0:0:0, 0, 0, 0, 0, 0, 43106, 29707)\n",
      "Weight(1995-9-25 0:0:0, 2, 0, 0, 3, 0, 51728, 35721)\n",
      "Weight(1996-5-27 0:0:0, 5, 0, 0, 0, 5, 103456, 71393)\n",
      "Weight(1997-8-25 0:0:0, 5, 0, 0, 2, 0, 155184, 107163)\n",
      "Weight(1999-10-18 0:0:0, 0, 0, 0, 6, 0, 155184, 107163)\n",
      "Weight(2000-11-6 0:0:0, 0, 3, 8, 0, 0, 194582, 139312)\n",
      "Weight(2002-7-23 0:0:0, 0, 0, 0, 1.5, 0, 194582, 140936)\n",
      "Weight(2003-9-29 0:0:0, 0, 0, 0, 1.5, 0, 194582, 140936)\n",
      "Weight(2007-6-20 0:0:0, 1, 0, 0, 0, 0, 208676, 155019)\n",
      "Weight(2007-12-31 0:0:0, 0, 0, 0, 0, 0, 229341, 175682)\n",
      "Weight(2008-1-21 0:0:0, 0, 0, 0, 0, 0, 229341, 175682)\n",
      "Weight(2008-6-26 0:0:0, 0, 0, 0, 0, 0, 229341, 204652)\n",
      "Weight(2008-6-27 0:0:0, 0, 0, 0, 0, 0, 238880, 214200)\n",
      "Weight(2008-10-31 0:0:0, 3, 0, 0, 0.335, 0, 310543, 278461)\n",
      "Weight(2009-6-22 0:0:0, 0, 0, 0, 0, 0, 310543, 292367)\n",
      "Weight(2009-6-30 0:0:0, 0, 0, 0, 0, 0, 310543, 292376)\n",
      "Weight(2009-10-15 0:0:0, 0, 0, 0, 0, 0, 310543, 292411)\n",
      "Weight(2010-6-28 0:0:0, 0, 0, 0, 0, 0, 310543, 310537)\n",
      "Weight(2010-9-17 0:0:0, 0, 0, 0, 0, 0, 348501, 310537)\n",
      "Weight(2011-8-5 0:0:0, 0, 0, 0, 0, 0, 512335, 310536)\n",
      "Weight(2011-12-31 0:0:0, 0, 0, 0, 0, 0, 512335, 310536)\n",
      "Weight(2012-10-19 0:0:0, 0, 0, 0, 1, 0, 512335, 310536)\n",
      "Weight(2012-12-31 0:0:0, 0, 0, 0, 0, 0, 512335, 310536)\n",
      "Weight(2013-6-20 0:0:0, 6, 0, 0, 1.7, 0, 819736, 496857)\n",
      "Weight(2013-11-12 0:0:0, 0, 0, 0, 0, 0, 819736, 557590)\n",
      "Weight(2014-1-9 0:0:0, 0, 0, 0, 0, 0, 952075, 557590)\n",
      "Weight(2014-6-12 0:0:0, 0, 0, 0, 1.6, 2, 1.14249e+06, 669106)\n",
      "Weight(2014-9-1 0:0:0, 0, 0, 0, 0, 0, 1.14249e+06, 983671)\n",
      "Weight(2015-4-13 0:0:0, 0, 0, 0, 1.74, 2, 1.37099e+06, 1.18041e+06)\n",
      "Weight(2015-5-21 0:0:0, 0, 0, 0, 0, 0, 1.43087e+06, 1.18041e+06)\n",
      "Weight(2016-5-23 0:0:0, 0, 0, 0, 0, 0, 1.43087e+06, 1.21927e+06)\n",
      "Weight(2016-6-16 0:0:0, 0, 0, 0, 1.53, 2, 1.71704e+06, 1.46312e+06)\n",
      "Weight(2017-1-9 0:0:0, 0, 0, 0, 0, 0, 1.71704e+06, 1.6918e+06)\n",
      "Weight(2017-7-21 0:0:0, 0, 0, 0, 1.58, 0, 1.71704e+06, 1.6918e+06)\n",
      "Weight(2017-12-31 0:0:0, 0, 0, 0, 0, 0, 1.71704e+06, 1.6918e+06)\n",
      "Weight(2018-5-21 0:0:0, 0, 0, 0, 0, 0, 1.71704e+06, 1.71703e+06)\n",
      "Weight(2018-7-12 0:0:0, 0, 0, 0, 1.36, 0, 1.71704e+06, 1.71703e+06)\n"
     ]
    }
   ],
   "source": [
    "ws = sm['sz000001'].getWeight()\n",
    "for w in ws:\n",
    "    print(w)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
